// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import MapData from "../map/base/MapData";
@ccclass
export default class NewClass extends cc.Component {
    @property(cc.EditBox)
    dataJsonEdit:cc.EditBox = null;
    @property(cc.EditBox)
    imgBgEdit:cc.EditBox = null;
    // LIFE-CYCLE CALLBACKS:
    private bgImg:cc.Texture2D = null;
    private _mapData:MapData = null;

    start () {
        let self = this;
        this.bgImg = new cc.Texture2D();
        cc.director.on("onLoadImgOver",function(data){
            if(data)
                self.setMapInfo(data);
        });

        cc.director.on("onLoadJsonOver",function(result){
            if(result && result.data){
                self._mapData = JSON.parse(result.data);
                self.dataJsonEdit.string = result.name;
            }
        });
    }

    //设置加载的地图信息
    setMapInfo(data:any){
        if(this.node.active == true){        
            this.bgImg.initWithElement(data.img);
            this.imgBgEdit.string = data.name;
        }
    }

    onOkClick(){
        this.node.active = false;
        let sceneMap = cc.find("Canvas/SceneMap").getComponent("SceneMap");
        sceneMap.init(this._mapData,this.bgImg);
    }

    onCloseClick(){
        this.node.active = false;
    }

    onOpenFileClick(){
        document.getElementById("filejson").click();
    }
    // update (dt) {}
}
